﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;

namespace ExcelAddIn1
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            TestSelectForm f = new TestSelectForm();
            //NativeWindow xlMain = new NativeWindow();
            //xlMain.AssignHandle(new IntPtr(Globals.ThisAddIn.Application.Hwnd));
            //f.ShowDialog(xlMain);
            //xlMain.ReleaseHandle();
            f.ShowDialog();

            int testID = f.GetTestID();
            if (testID != -1)
            {
                Excel.Range dataRange = (Excel.Range)Globals.ThisAddIn.Application.InputBox("Select the cells containing the input data", "Input range", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
                Excel.Range outArea = (Excel.Range)Globals.ThisAddIn.Application.InputBox("Select the cell where to place the output", "Output area", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);

                try
                {
                    DataContainer data = new DataContainer();
                    data.CreateFromExcel(dataRange);

                    Test test = Test.GetTest(testID, data);
                    test.SetAlpha(f.GetAlpha());
                    test.SetMeanDifference(f.GetMeanDifference());
                    if (test != null)
                    {
                        //Run the actual test
                        test.RunTest();

                        //Print the result to Excel
                        List<String> res = test.GetResult();
                        int r = 1;
                        foreach (String s in res)
                        {
                            String[] tokens = s.Split(';');
                            for (int c = 0; c < tokens.Length; c++)
                            {
                                if (!tokens[c].Equals("-"))
                                {
                                    outArea.Cells[r, c + 1].Value2 = tokens[c];
                                    if (s.EndsWith(";-"))
                                    {
                                        outArea.Cells[r, c + 1].Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
                                    }
                                }
                            }
                            r++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //An error occured
                    outArea.Cells[1, 1].Value2 = "Error!";
                    outArea.Cells[2, 1].Value2 = ex.Message;
                }
            }
        }
    }
}
